package com.yonder.gocommand.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yonder.gocommand.entity.Company;
import com.yonder.gocommand.entity.Item;
import com.yonder.gocommand.entity.Order;
import com.yonder.gocommand.entity.OrderStatus;
import com.yonder.gocommand.entity.User;
import com.yonder.gocommand.service.ICompanyService;
import com.yonder.gocommand.service.IItemService;
import com.yonder.gocommand.service.IOrderService;
import com.yonder.gocommand.service.IUserService;

@Controller
@RequestMapping("/")
public class BaseController {

	private IUserService userService;
	private IItemService itemService;
	private ICompanyService companyService;
	private IOrderService orderService;

	@Autowired
	public BaseController(IUserService userService, IItemService itemService, ICompanyService companyService, IOrderService orderService) {
		this.userService = userService;
		this.itemService = itemService;
		this.companyService = companyService;
		this.orderService = orderService;
	}

	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String welcome(ModelMap model) {
		model.addAttribute("message", "Maven Web Project + Spring 3 MVC - welcome()");

		// Spring uses InternalResourceViewResolver and return back index.jsp
		return "index";
	}

	@RequestMapping(value = "/welcome/{name}", method = RequestMethod.GET)
	public String welcomeName(@PathVariable String name, ModelMap model) {

		model.addAttribute("message", "Maven Web Project + Spring 3 MVC - " + name);

		// Test DAO for User
		User user = new User();
		user.seteMail("cosmin_spn09@yahoo.com");
		user.setFirstName("Cosmin");
		user.setLastName("Spinu");
		user.setUsername("cosmin");
		user.setPassword("parola");
		userService.saveOrUpdate(user);

		// Test DAO for Item
		Item item = new Item();
		item.setDescription("branza foarte buna");
		item.setName("branza");
		item.setPrice((long) 10.5);
		itemService.saveOrUpdate(item);

		// Test DAO for Company
		Company company = new Company();
		company.setTelephone("0232111111");
		companyService.saveOrUpdate(company);
		
		// Test DAO for Order
		Order order = new Order();
		order.setOrderStatus(OrderStatus.NEW);
		orderService.saveOrUpdate(order);

		return "index";
	}
}
